//
//  NBAPersonnelDept.swift
//  Sports
//
//  Created by tiger on 2017/10/17.
//  Copyright © 2017年 Meigabay. All rights reserved.
//

import UIKit

public class NBAPersonnelDept: NBADepartment {
    public var boss: NBAChairman!
    
    init() {
        
    }
    
    /// 初始化 人事部
    internal func initPersonnelData() {
        //创建 联盟主席
        self.createChairman()
        //创建联盟俱乐部
        self.createClubs()
        //创建联盟教练池
        self.createCoachs()
        //创建联盟裁判池
        self.createReferees()
        //创建联盟球员池
        self.createPlayers()
    }
    
    private func createChairman() {
        let chairMan = NBAChairman()
        chairMan.workerId = String(format: "chairman_%0.0f",NSDate().timeIntervalSince1970*1000)
        chairMan.name = "肖华"
        chairMan.nickname = "亚当.肖华"
        chairMan.gender = .men
        chairMan.introduction = "肖华在1992年开始在NBA工作，在担任现在总裁职务之前，萧华担任过委员助理，员工主席，NBA娱乐公司的副总裁和总裁。肖华还曾担任iMax电影Michael Jordan to the Max的执行制作，，以及TNT纪录片Whatever Happened to Micheal Ray?的执行制作。他还参与了Like Mike和Year of the Yao。在2003年，萧华被时代杂志和CNN誉为全球最有影响力商人之一;他也被多次被The Sporting News誉为运动界百大最有影响力。"
        chairMan.drafted = 35
        chairMan.birthDay = -242640000
        chairMan.height = 183
        chairMan.weight = 75
        chairMan.type = .chairman
        do {
            try NBADBManager.shared.insertWorker(worker: chairMan)
        } catch {
            
        }
    }
    
    /// 创建联盟俱乐部
    ///
    /// - Returns: 俱乐部
    private func createClubs() {
        let clubIds = ["club_100","club_101","club_102","club_103","club_104","club_105","club_106","club_107"]
        let clubCNNames = ["雷霆","火箭","骑士","马刺","勇士","凯尔特人","湖人","76人"]
        let clubPlaces = ["奥克兰","休斯敦","克利夫兰","圣安东尼奥","金州","波士顿","洛杉矶","费城"]
        let clubVenues = ["贺龙体育馆","丰田中心","金州大道体育馆","健力宝中心","鸟巢体育馆","铁岭中心","华尔街大剧院","汽车城4S店"]
        let clubENNames = ["Thunder","Rockets","Cavaliers","Spurs","Warriors","Celtics","Lakers","76ers"]
        let clubIcons = ["logo_oklahoma_city_thunder","logo_houston_rockets","log_cleveland_cavaliers","logo_san_antonio_spurs","logo_golden_state_warriors","logo_boston_celtics","logo_los_angeles_lakers","logo_philadelphia_76ers"]
        for index in 0..<clubIds.count {
            let club = NBAClub()
            club.clubId = clubIds[index]
            club.name = clubCNNames[index]
            club.city = clubPlaces[index]
            club.venue = clubVenues[index]
            club.icon = clubIcons[index]
            club.introduction = clubENNames[index]
            club.createTime = NSDate().timeIntervalSince1970
            do {
                try NBADBManager.shared.insertClub(club: club)
            } catch {
                
            }
        }
    }
    
    /// 创建俱乐部老板们
    private func createBoss() {
        let bossNames = ["丹尼","吉尔","蒂姆","哈吉","科维奇","欧阳慕容","海林斯","李春华"]
        let clubIds = ["club_100","club_101","club_102","club_103","club_104","club_105","club_106","club_107"]
        for index in 0..<bossNames.count {
            let boss = NBAClubBoss()
            boss.workerId = String(format: "boss_%0.0f",NSDate().timeIntervalSince1970*1000)
            boss.clubId = clubIds[index]
            boss.name = bossNames[index]
            boss.avatar = "boss_avatar"
            boss.birthDay = 64037532
            boss.type = .boss
            do {
                try NBADBManager.shared.insertClubBoss(boss: boss)
            } catch {
                
            }
        }
    }
    
    
    /// 创建教练池
    private func createCoachs() {
        let coachNames = ["托尼衣维奇","丘处机","张三丰","一灯大师","爱因斯坦","福尔摩斯","包大人","波波不维奇"]
        let clubIds = ["club_100","club_101","club_102","club_103","club_104","club_105","club_106","club_107"]
        for index in 0..<coachNames.count {
            let coach = NBAClubCoach()
            coach.workerId = String(format: "coach_%0.0f",NSDate().timeIntervalSince1970*1000)
            coach.clubId = clubIds[index]
            coach.name = coachNames[index]
            coach.nickname = coachNames[index]
            coach.avatar = "boss_avatar"
            coach.birthDay = 64037532
            coach.type = .coach
            do {
                try NBADBManager.shared.insertClubCoach(coach: coach)
            } catch {
                
            }
        }
    }
    
    ///创建裁判池
    private func createReferees() {
        let refereeNames = ["光头老者","小萌妹","滨崎川子","刘海生","金日成","黄药师","希特勒","海尔"]
        for index in 0..<refereeNames.count {
            let referee = NBAReferee()
            referee.workerId = String(format: "coach_%0.0f",NSDate().timeIntervalSince1970*1000)
            referee.name = refereeNames[index]
            referee.nickname = refereeNames[index]
            referee.avatar = "boss_avatar"
            referee.birthDay = 64037532
            referee.type = .referee
            do {
                try NBADBManager.shared.insertWorker(worker: referee)
            } catch {
                
            }
        }
    }
    
    ///创建球员池
    private func createPlayers() {
        
        /// 超级巨星
        let superStars = ["詹姆斯","杜兰特","维斯布鲁克","哈登","莱纳德","欧文","恩比德","库里"]
        /// 巨星
        let stars = ["乐福","汤普森","保罗乔治","保罗","阿尔德里奇","霍福德","西蒙斯","鲍尔"]
        /// 尖兵 有概率发挥潜力爆发
        let betterPlayers = ["托马斯","格林","安东尼","冰皇","海沃德","伊戈达拉","洛佩慈","库滋马"]
        /// 普通蓝领 有概率发挥潜力爆发
        let normalPlayers = ["杨过","欧阳峰","乔峰","一木花道","周琦","哈基姆","欧特","兰德里","坎特","亚当斯","吉诺比利","帕克"]
        /// 低级别联赛球员， 有概率发挥潜力爆发
        let lowPlayers = ["周琦","比斯利","林狐冲","流川枫","利文斯顿","周伯通","欧阳艾伦","JR","宇航丁"]
        
        let clubIds = ["club_100","club_101","club_102","club_103","club_104","club_105","club_106","club_107"]
        for index in 0..<clubIds.count {
            
            let player = self.createSuperStar(1)
            player.workerId = String(format: "player_%0.0f",NSDate().timeIntervalSince1970*1000)
            player.name = superStars[(Int(arc4random() % 8))]
            player.nickname = superStars[(Int(arc4random() % 8))]
            player.avatar = "boss_avatar"
            player.birthDay = 64037532
            player.type = .player
            player.clubId = clubIds[index]
            player.position = .c
            do {
                try NBADBManager.shared.insertPlayer(player: player)
            } catch {
                
            }
            
            player.workerId = String(format: "player_%0.0f",NSDate().timeIntervalSince1970*1000)
            player.name = stars[(Int(arc4random() % 8))]
            player.nickname = stars[(Int(arc4random() % 8))]
            do {
                try NBADBManager.shared.insertPlayer(player: player)
            } catch {
                
            }
            
            player.workerId = String(format: "player_%0.0f",NSDate().timeIntervalSince1970*1000)
            player.name = betterPlayers[(Int(arc4random() % 8))]
            player.nickname = betterPlayers[(Int(arc4random() % 8))]
            do {
                try NBADBManager.shared.insertPlayer(player: player)
            } catch {
                
            }
            
            player.workerId = String(format: "player_%0.0f",NSDate().timeIntervalSince1970*1000)
            player.name = normalPlayers[(Int(arc4random() % 8))]
            player.nickname = normalPlayers[(Int(arc4random() % 8))]
            do {
                try NBADBManager.shared.insertPlayer(player: player)
            } catch {
                
            }
            
            player.workerId = String(format: "player_%0.0f",NSDate().timeIntervalSince1970*1000)
            player.name = lowPlayers[(Int(arc4random() % 8))]
            player.nickname = lowPlayers[(Int(arc4random() % 8))]
            do {
                try NBADBManager.shared.insertPlayer(player: player)
            } catch {
                
            }
        }
    }
    
    private func createSuperStar(_ precent: Double) -> NBAPlayer {
        let player = NBAPlayer()
        player.workerId = String(format: "player_%0.0f",NSDate().timeIntervalSince1970*1000)
        player.avatar = "boss_avatar"
        player.birthDay = 64037532
        player.type = .player
        
        player.number = (Int(arc4random() % 100))
        //打球能力
        player.twoPoint = 0.45 // 2分球命中率
        player.threePoint = 0.45 // 3分球命中率
        player.controlAbility = 0.8 // 控球能力
        player.passAbility = 0.7 // 传球能力
        player.handlingStressAbility = 0.8 //抗压能力
        
        //身体素质
        player.bounceSpeed = 2 //弹跳速度 米/秒
        player.bounceHeight = 1  //弹跳高度 米
        player.runSpeed = 11 //  奔跑速度 米/秒
        player.thinkingSpeed = 0.03 // 思维速度秒
        player.physicalStrength = 1 // 体力值 [0-1] 百分比
        
        return player
    }
    
}

public class NBAClubPersonnelDept: NBADepartment {
    public private(set) var boss: NBAClubBoss! //boss是一定有的
    public private(set) var gm: NBAClubGM? //总经理不一定有
    public private(set) var coach: NBAClubCoach! //教练  必须要
    public private(set) var players: [NBAPlayer]! //球员
    
    init(club: NBAClub) {
        self.boss = NBADBManager.shared.filterBoss(clubId: club.clubId)
//        self.gm =
        self.players = NBADBManager.shared.filterClubPlayers(clubId: club.clubId)
//        self.coach =
    }
    
    /// 准备比赛
    internal func preparePlayersForGame(game: NBAGame) throws -> [NBAPlayer] {
        var wPlayers = [NBAPlayer]()
        for player in self.players {
            if wPlayers.count < 5 {
                wPlayers.append(player)
            } else {
                break
            }
        }
        return wPlayers
    }

    internal func prepareCoachForGame(game: NBAGame) {
        
    }
    
}

